#include "../head/page1.h"
#include"../head/find_way.h"
lv_obj_t *page1_prev=NULL;
static lv_obj_t *mbox_1;
lv_obj_t *page2;
lv_obj_t *page3;
int begin_place = 0;
int end_place = 0;
lv_obj_t *kb1 = NULL;
static lv_obj_t *candidate_cont = NULL;
static char pinyin_buffer[32] = {0};  // 临时拼音缓冲
static lv_obj_t *kb_1 = NULL;
 lv_obj_t *ta1;
 lv_obj_t *ta2;
void create_page2(){
    page1_prev = lv_scr_act(); //lv_obj_t *page1_prev = lv_scr_act();
    page2=lv_obj_create(NULL);
    lv_obj_set_size(page2,LV_HOR_RES,LV_VER_RES);
    lv_obj_t *label = lv_label_create(page2);
    

    lv_obj_t *btn2=lv_btn_create(page2);
    lv_obj_set_size(btn2,100,50);
    lv_obj_t *label2=lv_label_create(btn2);
    lv_label_set_text(label2,"返回");
    lv_obj_set_style_text_font(label2,chinese_font,0);
    lv_obj_align(btn2,LV_ALIGN_TOP_RIGHT,0,0);
    lv_obj_add_event_cb(btn2, return_main_page, LV_EVENT_CLICKED, NULL);

    lv_label_set_text(label,"请选择支付方式");
    lv_obj_align(label,LV_ALIGN_TOP_MID,0,50);
    lv_obj_set_style_text_font(label,chinese_font,0);

    lv_obj_t *label3 = lv_label_create(page2);
    char buf1[64];
    sprintf(buf1, "您乘坐%d站,需要支付%d元", count_use, count_use*2);
    lv_label_set_text(label3, buf1);
    lv_obj_align(label3, LV_ALIGN_TOP_MID, 0, 380);  
    lv_obj_set_style_text_font(label3, chinese_font, 0);
    create_page2_ui();
}

void return_main_page2(lv_event_t *e){
    lv_scr_load(page1_prev);
    kb_1 = NULL;
}












PlaceMap place_map[] = {
    {"番禺广场", 1},
    {"市桥", 2},
    {"汉溪长隆", 3},
    {"广州塔", 4},
    {"珠江新城", 5},
    {"体育西", 6},
    {"天河客运站", 7},
    {"林和西", 8},
    {"燕塘", 9},
    {"嘉禾望岗", 10},
    {"高增", 11},
    {"机场北", 12},
    {"华师", 13},
    {"岗顶", 14},
    {"白云大道北", 15},
    {"人和", 16},
    {"广大", 17},
    {"广美", 18},
    {"广工", 19},
};


static const pinyin_entry_t pinyin_dict[] = {
    {"a", "啊阿呵腌吖嗄"},
    {"ai", "爱矮挨碍艾哎唉"},
    {"an", "安暗岸俺按案鞍"},
    {"ang", "昂盎肮仰"},
    {"ba", "把八吧爸巴拔罢霸"},
    {"bai", "白百柏摆败拜稗"},
    {"ban", "办半班般板版伴拌"},
    {"bang", "帮邦榜膀绑棒傍"},
    {"bao", "包保报抱暴宝胞饱"},
    {"bei", "北背贝倍杯被辈狈"},
    {"ben", "本奔苯笨畚夯"},
    {"beng", "崩绷蹦甭迸"},
    {"bi", "比必闭笔毕壁避碧彼毙"},
    {"bian", "变便边编辨辩扁贬"},
    {"biao", "表标彪膘飚镖"},
    {"bie", "别憋鳖瘪"},
    {"bin", "宾滨彬缤鬓"},
    {"bing", "并病兵冰柄饼丙"},
    {"bo", "波播博拨柏脖膊伯驳薄"},
    {"bu", "不部步补布簿哺"},
    {"ca", "擦嚓礤"},
    {"cai", "才菜采财裁猜踩"},
    {"can", "参餐残蚕惭掺璨"},
    {"cang", "藏仓苍沧舱"},
    {"cao", "草操糙曹嘈"},
    {"ce", "策侧册测厕"},
    {"cen", "岑涔"},
    {"ceng", "曾层蹭"},
    {"cha", "查差插茶察叉搽"},
    {"chai", "拆差柴豺"},
    {"chan", "产缠颤铲阐禅蝉"},
    {"chang", "长常场厂唱昌畅倡"},
    {"chao", "超朝抄吵潮巢"},
    {"che", "车撤扯掣澈"},
    {"chen", "陈晨沉趁尘辰衬衬"},
    {"cheng", "成程城承乘呈诚骋"},
    {"chi", "吃持池迟赤齿翅驰"},
    {"chong", "重充冲虫崇宠"},
    {"chou", "抽愁臭仇酬筹"},
    {"chu", "出初除础储触厨雏"},
    {"chuai", "揣膪"},
    {"chuan", "传船穿川串椽"},
    {"chuang", "创床窗闯疮"},
    {"chui", "吹垂炊捶锤"},
    {"chun", "春纯醇唇椿淳"},
    {"chuo", "戳绰踔"},
    {"ci", "次此词差刺慈磁雌"},
    {"cong", "从丛聪葱囱匆"},
    {"cu", "促粗簇醋"},
    {"cuan", "窜篡攒汆"},
    {"cui", "催脆翠崔瘁"},
    {"cun", "存村寸皴"},
    {"cuo", "错措挫磋撮搓"},
    {"da", "大打达答搭瘩耷"},
    {"dai", "带代待戴贷袋逮"},
    {"dan", "但单担淡蛋胆诞旦弹"},
    {"dang", "当党挡荡档裆"},
    {"dao", "到道导岛盗稻悼"},
    {"de", "的得德"},
    {"deng", "等灯登凳邓瞪蹬"},
    {"di", "地第低敌递抵弟帝缔"},
    {"dian", "点电店典垫殿淀颠甸"},
    {"diao", "调掉吊雕钓叼刁"},
    {"die", "叠蝶碟爹蹀"},
    {"ding", "定丁顶订盯鼎锭"},
    {"diu", "丢铥"},
    {"dong", "东动冬懂洞董栋侗"},
    {"dou", "都斗抖豆逗陡"},
    {"du", "都度独毒读杜渡堵"},
    {"duan", "段短断端锻缎"},
    {"dui", "对队堆兑碓"},
    {"dun", "顿盾蹲敦吨"},
    {"duo", "多夺朵躲垛惰舵剁"},
    {"fan", "番凡翻繁范返"},
    {"yu",  "禺育玉雨羽鱼于余宇"},
    {"guang","广光逛惯光光"},
    {"chang","场长常畅厂昌"},
    {"shi", "市十使识士时事试师室"},
    {"qiao","桥巧瞧悄侨乔"},
    {"han", "汉涵寒汗罕憨"},
    {"xi",  "溪西息洗喜细昔习"},
    {"long", "隆龙垄笼聋咙"},
    {"ta",  "塔踏他它塌"},
    {"zhu", "珠竹诸助驻祝"},
    {"jiang","江疆奖将蒋浆"},
    {"xin", "新心信欣芯辛"},
    {"cheng","城承呈程诚乘"},
    {"ti",  "体梯提替惕"},
    {"he",  "河合荷和呵"},
    {"ke",  "客可克科柯"},
    {"yun","运云匀韵蕴"},
    {"zhan","站占湛瞻栈"},
    {"lin", "林琳霖磷凛"},
    {"yan", "燕岩沿炎艳"},
    {"tang","塘唐糖堂"},
    {"jia", "嘉加架甲家"},
    {"wang","望王网旺往"},
    {"gang","岗刚钢缸港"},
    {"gao", "高告搞膏羔"},
    {"zeng","增曾憎赠缯"},
    {"ji",  "机集及济几吉季"},
    {"bei", "北背贝倍辈狈"},
    {"hua", "华花画化滑桦"},
    {"ding","顶定丁钉鼎"},
    {"bai", "白百柏摆拜"},
    {"da","大达打答瘩"},
    {"dao","道导岛盗稻"},
    {"ren", "人仁认任忍纫"},
    {"mei", "美每眉媒魅"},
    {"gong","工公功攻宫"},
    {"pan", "盘判潘盼畔番"},
    {"zhou","州洲周轴"},
    {NULL, NULL}
};




//----------------候选条点击事件-----------------
static void candidate_click_cb(lv_event_t * e) {
    lv_obj_t *label = lv_event_get_target(e);      
    lv_obj_t *ta_1 = lv_event_get_user_data(e);      

    const char *txt = lv_label_get_text(label);
    int blen= strlen(pinyin_buffer);
    lv_obj_t *ta_use=ta_1;
    printf("点击：%s\n",pinyin_buffer);
    fflush(stdout);
    memset(pinyin_buffer,0,sizeof(pinyin_buffer)); //清空拼音缓冲
    // 直接插入光标位置
    lv_textarea_add_text(ta_use, txt);
    
    //光标左移一格
    lv_textarea_set_cursor_pos(ta_1, lv_textarea_get_cursor_pos(ta_1) - 1);

    for(int i=0 ; i<blen;i++){
        lv_textarea_del_char(ta_1);
    }



        //光标右移一格
    lv_textarea_set_cursor_pos(ta_1, lv_textarea_get_cursor_pos(ta_1) + 1);

    printf("点击：%s\n", txt);
    fflush(stdout);

    lv_obj_add_flag(candidate_cont, LV_OBJ_FLAG_HIDDEN);
}




//----------------显示候选条-----------------
/* 获取 UTF-8 字符的字节长度 */
/*UTF-8是一种变长的存储方式，可以通过码位值来看占用几个字节*/
static int utf8_char_size(const char *p) {
    unsigned char c = (unsigned char)p[0];
    if(c < 0x80) return 1;        // 0xxxxxxx
    else if((c & 0xE0) == 0xC0) return 2; //C: 110xxxxx  此时的0xE0是11100000 掩码操作取前三位 2字节
    else if((c & 0xF0) == 0xE0) return 3; //E: 1110xxxx 取前四位 得到三位的话说明是3字节
    else if((c & 0xF8) == 0xF0) return 4; //F8: 11110xxx 前五位 4字节
    return 1; // 出错时退化为1字节
}

static void show_candidates(lv_obj_t * ta_1, const char *pinyin) {
    if(candidate_cont == NULL) {
        candidate_cont = lv_obj_create(lv_layer_top());

        lv_obj_set_size(candidate_cont, 240, 40);
        lv_obj_align(candidate_cont, LV_ALIGN_OUT_TOP_LEFT, 0, 150); // 键盘上方

        // 横向滚动容器
        lv_obj_set_scroll_dir(candidate_cont, LV_DIR_HOR);
        lv_obj_set_scrollbar_mode(candidate_cont, LV_SCROLLBAR_MODE_AUTO);
        lv_obj_set_flex_flow(candidate_cont, LV_FLEX_FLOW_ROW);
        lv_obj_set_style_pad_all(candidate_cont, 2, 0);
    }

    lv_obj_clean(candidate_cont); 
    lv_obj_clear_flag(candidate_cont, LV_OBJ_FLAG_HIDDEN);

    for (int i = 0; pinyin_dict[i].pinyin != NULL; i++) {
        if (strcmp(pinyin, pinyin_dict[i].pinyin) == 0) {
            const char *cands = pinyin_dict[i].candidates;
            int j = 0;
            while(cands[j] != '\0') {//遍历候选词(每次循环写一个"字")
                char buf[8] = {0};
                int len = utf8_char_size(&cands[j]); 
                // 使用自己写的 UTF-8 字符长度函数  得到字节长度
                memcpy(buf, &cands[j], len);
                //这样buf就是一个合法c语言风格字符串 可以直接打印
                buf[len] = '\0';
                j += len;
                //参考博客:https://blog.csdn.net/whahu1989/article/details/118314154
                lv_obj_t *lbl = lv_label_create(candidate_cont);
                lv_label_set_text(lbl, buf);
                lv_obj_set_style_text_font(lbl, chinese_font, 0);

                lv_obj_add_flag(lbl, LV_OBJ_FLAG_CLICKABLE);
                lv_obj_add_event_cb(lbl, candidate_click_cb, LV_EVENT_CLICKED, ta_1);

                lv_obj_set_style_pad_hor(lbl, 5, 0);
            }
            return;
        }
    }
}


//----------------输入框事件-----------------
static void ta_event_cb_2(lv_event_t *e) {
    lv_obj_t *ta_1 = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_FOCUSED) {
        if(kb_1) {
            lv_keyboard_set_textarea(kb_1, ta_1);
            lv_obj_clear_flag(kb_1, LV_OBJ_FLAG_HIDDEN);
        }
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        const char *txt = lv_textarea_get_text(ta_1);
        int len = strlen(txt);
        if(len > 0) {
            char last = txt[len-1];
            if((last >= 'a' && last <= 'z') || (last >= 'A' && last <= 'Z')) {
                int blen = strlen(pinyin_buffer);
                if(blen < sizeof(pinyin_buffer)-1) {
                    pinyin_buffer[blen] = last;
                    pinyin_buffer[blen+1] = '\0';
                }
                show_candidates(ta_1, pinyin_buffer);
            }
        }
    }
}

//----------------键盘隐藏-----------------
void ta_event_hide_cb_2(lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL || code == LV_EVENT_DEFOCUSED) {
        if(kb_1) lv_obj_add_flag(kb_1, LV_OBJ_FLAG_HIDDEN);
    } 
}


//----------------辅助函数-----------------
int get_node_id(const char *place_name) {
    for(int i = 0; i < place_count; i++) {
        if(strcmp(place_name, place_map[i].name) == 0) {
            return place_map[i].node_id;
        }
    }
    return -1;
}

void btn_event_cb_2(lv_event_t * e) {
    lv_obj_del(kb_1);
    kb_1=NULL;//键盘
    login_input_t *inputs = lv_event_get_user_data(e);

    const char *start_name = lv_textarea_get_text(inputs->ta_username);
    const char *end_name   = lv_textarea_get_text(inputs->ta_password);

    int start = get_node_id(start_name);
    int end   = get_node_id(end_name);

    if(start == -1 || end == -1) {
        lv_obj_t *mbox = lv_msgbox_create(lv_scr_act(), "错误", "请输入正确的站点名称", NULL, true);
        lv_obj_set_style_text_font(mbox, chinese_font, 0);
        lv_obj_center(mbox);
        return;
    }

    BFS_shortest_path(&graph, start, end);


    /*static const char *btns[] = {"重设", "ok", ""};
                lv_obj_t *mbox=lv_msgbox_create(lv_scr_act(),"login","密码错误",btns,true);
                lv_obj_align(mbox,LV_ALIGN_TOP_MID,0,100);
                lv_obj_set_style_text_font(mbox, chinese_font, 0);
              lv_obj_center(mbox);*/


    strcat(visit_place,"\n是否跳转支付?");
    mbox_1 = lv_msgbox_create(lv_scr_act(), NULL, visit_place, NULL, true); 
     lv_obj_align(mbox_1, LV_ALIGN_TOP_MID, 0, 100);
    lv_obj_set_style_text_font(mbox_1, chinese_font, 0);
    lv_obj_set_height(mbox_1, 300);
    lv_obj_set_width(mbox_1, 500);
    lv_obj_center(mbox_1);

    

    // 添加底部按钮
    lv_obj_t *btn = lv_btn_create(mbox_1);         // 在消息盒子上创建按钮
    lv_obj_set_size(btn, 100, 50);
    lv_obj_align(btn, LV_ALIGN_BOTTOM_MID, 0, -10); 
    lv_obj_t *label = lv_label_create(btn);
    lv_label_set_text(label, "跳转");
    lv_obj_center(label);

    // 设置字体
    lv_obj_set_style_text_font(label, chinese_font, 0);

    // 添加事件回调
    lv_obj_add_event_cb(btn, jump_to_pay, LV_EVENT_CLICKED, NULL);



    
     
    
    


}



void img_click_close3(lv_event_t *e){
    lv_obj_t *img = lv_event_get_target(e);
    printf("图片11被点击\n");
    fflush(stdout);
    lv_obj_del(img);
}

//显示站点
static void show_img_event_cb(lv_event_t * e)
{
    // 创建图片对象
    lv_obj_t * img = lv_img_create(lv_scr_act());
    lv_img_set_src(img, "/picture/station.bmp");

    lv_obj_add_flag(img, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(img, img_click_close3,LV_EVENT_CLICKED,NULL);

    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}




//跳转至支付----------------------------------------  
void jump_to_pay(lv_event_t *e){
    printf("此时flag=%d\n",usersave);
    // target 是按钮
    if((users[usersave].flag)==0){
    create_page2();
    lv_obj_t *btn = lv_event_get_target(e);
    lv_scr_load(page2);
    users[usersave].flag=1;
    }else{
        lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act()," ","你已经买过票了,不可重复买票",NULL,false);
        lv_obj_set_size(msgbox, 500, 300);
        lv_obj_set_style_text_font(msgbox,chinese_font,0);
        lv_obj_align(msgbox,LV_ALIGN_CENTER,0,0);

        lv_obj_t *btn2=lv_btn_create(msgbox);
        lv_obj_set_size(btn2,100,50);
        lv_obj_t *label2=lv_label_create(btn2);
        lv_label_set_text(label2,"好的");
        lv_obj_set_style_text_font(label2,chinese_font,0);
        lv_obj_add_event_cb(btn2, return_main_page, LV_EVENT_CLICKED,NULL);

    }

    printf("跳转按钮被点击\n");

}



void ta_del_from_pinyin(lv_event_t *e){
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *kb3_1 = lv_event_get_target(e);
    const char *txt1=lv_btnmatrix_get_btn_text(kb3_1,lv_btnmatrix_get_selected_btn(kb3_1));
    if(strcmp(txt1,LV_SYMBOL_BACKSPACE)==0){
        memset(pinyin_buffer,0,sizeof(pinyin_buffer));
    }

}



void enable_chinese_input(lv_obj_t *ta_1) {
    
        kb_1 = lv_keyboard_create(lv_scr_act());
        lv_obj_align(kb_1, LV_ALIGN_BOTTOM_MID, 0, 0);
        lv_keyboard_set_textarea(kb_1, ta_1);
        lv_obj_add_flag(kb_1, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_event_cb(kb_1, ta_del_from_pinyin, LV_EVENT_VALUE_CHANGED, NULL);
        lv_obj_add_event_cb(kb_1, ta_event_hide_cb_2, LV_EVENT_READY, NULL);
        lv_obj_add_event_cb(kb_1, ta_event_hide_cb_2, LV_EVENT_CANCEL, NULL);
        lv_obj_add_event_cb(kb_1, ta_event_hide_cb_2, LV_EVENT_DEFOCUSED, NULL);
        setup_hide_kb(kb_1);
    

    lv_obj_add_event_cb(ta_1, ta_event_cb_2, LV_EVENT_FOCUSED, NULL);
    lv_obj_add_event_cb(ta_1, ta_event_cb_2, LV_EVENT_VALUE_CHANGED, NULL);
}


void img_click_turn_to_page3(lv_event_t *e){
    create_page3();
    lv_scr_load(page3);
    
}



//----------------UI创建-----------------

void create_page1_ui(void) {
    login_input_t *here_input = malloc(sizeof(login_input_t));

    // 起始位置输入框
    ta1 = lv_textarea_create(page1);
    lv_obj_set_size(ta1, 200, 40);
    lv_obj_align(ta1, LV_ALIGN_TOP_MID, 0, 40);
    lv_textarea_set_placeholder_text(ta1, "请输入起始位置");
    lv_obj_set_style_text_font(ta1, chinese_font, 0);
    here_input->ta_username = ta1;


    //--------------------------------------------推荐图片与调用界面三

    lv_obj_t *img = lv_img_create(page1);
    lv_img_set_src(img,"/picture/tuijian.bmp");
    if(lv_img_get_src(img) == NULL) {
        printf("图片加载失败:/picture/tuijian.bmp\n");
        lv_obj_del(img);
        return;
    }
    printf("图片加载成功: /picture/tuijian.bmp");
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 150);

    lv_obj_add_flag(img, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(img, img_click_turn_to_page3,LV_EVENT_CLICKED,NULL);

    // 启用中文输入
    enable_chinese_input(ta1);

    // 目的地输入框
    ta2 = lv_textarea_create(page1);
    lv_obj_set_size(ta2, 200, 40);
    lv_obj_align(ta2, LV_ALIGN_TOP_MID, 0, 100);
    lv_textarea_set_placeholder_text(ta2, "请输入目的地");
    lv_obj_set_style_text_font(ta2, chinese_font, 0);
    here_input->ta_password = ta2;

    // 启用中文输入
    enable_chinese_input(ta2);

    // 查询按钮
    lv_obj_t *btn_search = lv_btn_create(page1);
    lv_obj_align(btn_search, LV_ALIGN_TOP_MID, 0, 160);
    lv_obj_t *btn_label = lv_label_create(btn_search);
    lv_label_set_text(btn_label, "查询路线");
    lv_obj_set_style_text_font(btn_label, chinese_font, 0);
    lv_obj_center(btn_label);
    lv_obj_add_event_cb(btn_search, btn_event_cb_2, LV_EVENT_CLICKED, here_input);
    //显示站点:
    lv_obj_t * btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn, 100, 50);
    lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, 0);  // 左上角
    lv_obj_t * label = lv_label_create(btn);
    lv_label_set_text(label, "显示站点");
    lv_obj_set_style_text_font(label, chinese_font, 0);
    lv_obj_center(label);
    lv_obj_add_event_cb(btn, show_img_event_cb, LV_EVENT_CLICKED, NULL);


}
